/*
package com.travel.websocket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.travel.dao.message.MessageDAO;
import com.travel.dao.message.impl.MessageDAOlmpl;
import com.travel.pojo.message.Message;
import com.travel.utils.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;


@ServerEndpoint("/websocket/{userId_roomid}")
@Component
public class WebSocket {

    private MessageDAO messageDAO;

    //日志
    private Logger logger= LoggerFactory.getLogger(this.getClass());

    //会话
    private Session session;

    //用户id
    private Long userId;

    //房间号id
    private Integer tid;

    private static final Map<Integer, CopyOnWriteArraySet<WebSocket>> rooms = new HashMap<>();


    */
/**
     * 注入MessageDAO
     *//*

    public void init(){
        messageDAO = BeanUtil.getBean(MessageDAOlmpl.class,"messageDAO");
    }

    */
/**
     * 建立连接
     * @param userId_roomid 用户id和房间号用"-"隔开，
     *//*

    @OnOpen
    public void onOpen(@PathParam("userId_roomid")String userId_roomid, Session session){
        this.session=session;
        //以"-"为分隔符取id和房间号
        String[] param=userId_roomid.split("-");
        this.userId=Long.parseLong(param[0]);
        this.tid=Integer.parseInt(param[1]);
        CopyOnWriteArraySet<WebSocket> room = rooms.get(tid);
        if (room==null){
            synchronized (rooms){
                if (!rooms.containsKey(tid)){
                    room=new CopyOnWriteArraySet<>();
                    rooms.put(tid,room);
                }
            }
        }
        room.add(this);
        init();
    }

    */
/**
     * 发生错误
     *//*

    @OnError
    public void onError(Session session, Throwable error) {
        this.session=session;
        logger.info("服务端错误:"+error.getMessage());
        error.printStackTrace();
    }

    */
/**
     * 连接关闭
     *//*

    @OnClose
    public void onClose() {
        CopyOnWriteArraySet<WebSocket> room = rooms.get(tid);
        if (room!=null){
            room.remove(this);
        }
    }

    */
/**
     * 收到客户端的消息
     * msgType用于判断消息是聊天消息还是团公告
     * @param message 收到客户端的消息
     *//*

    @OnMessage
    public void onMessage(String message, Session session) throws IOException {
        this.session=session;
        JSONObject jsonObject = JSON.parseObject(message);
        String msg = jsonObject.getString("msg");
        Integer msgType=jsonObject.getInteger("msgType");

        CopyOnWriteArraySet<WebSocket> room = rooms.get(tid);

        Map<String,Object> map=new HashMap<>();
        map.put("msg",msg);
        map.put("msgType",msgType);
        map.put("fromid",userId);
        org.json.JSONObject jsonObject1=new org.json.JSONObject(map);
        String message1=jsonObject1.toString();

        if (room!=null){
            //循环发送给在一个聊天室的人
            for (WebSocket item:room){
                item.session.getAsyncRemote().sendText(message1);
            }
        }
        Message message2 = new Message.Builder()
                .setFromId(userId)
                .setMsg(msg)
                .setTid(tid)
                .setMsgType(msgType)
                .build();
        logger.info("成功发送一条群发消息");

        if (msgType==0|msgType==1){
            this.messageDAO.saveMessage(message2);
        }
    }


}
*/
